﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ListaCasamentoCL;


namespace ListaCasamento.adm
{
    public partial class Presentes : System.Web.UI.Page
    {
        private System.Data.DataTable _dtLinks
        {
            get {
                if (ViewState["VSLinks"] != null)
                    return (System.Data.DataTable)ViewState["VSLinks"];
                else
                    return null;
            }
            set
            {
                ViewState.Add("VSLinks", value);
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            CarregarPresentes();   
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Presente objPresente = new Presente();
                objPresente.Nme_Presente = txtNome.Text;
                objPresente.Des_Modelo_Presente = txtModelo.Text;
                objPresente.Save();
                SalvarImagem(objPresente.Idf_Presente.Value);

                if (_dtLinks != null)
                {
                    SugestoesPresente.DeleteAll(objPresente.Idf_Presente.Value);
                    System.Data.DataTable dt = (System.Data.DataTable)_dtLinks;
                    foreach (System.Data.DataRow dtRow in dt.Rows)
                    {
                        SugestoesPresente obj = new SugestoesPresente();
                        obj.Idf_Presente = objPresente.Idf_Presente.Value;
                        obj.Nme_Loja = dtRow["Loja"].ToString();
                        obj.Des_Link = dtRow["Link"].ToString();
                        obj.Save();
                    }
                }

                Response.Redirect("Presentes.aspx");
            }
        }

        private void SalvarImagem(int id)
        {
            if (fuFoto.HasFile)
                fuFoto.SaveAs(Server.MapPath("/images/presentes/" + id.ToString() + ".jpg"));
        }

        protected void btnAdicionarLink_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtLoja.Text) && !string.IsNullOrEmpty(txtLink.Text))
            {
                System.Data.DataTable dt = null;
                
                if (_dtLinks != null)
                {
                    dt = (System.Data.DataTable)_dtLinks;
                }
                else
                {
                    dt = new System.Data.DataTable();
                    dt.Columns.Add("Loja");
                    dt.Columns.Add("Link");
                }

                System.Data.DataRow dRow = dt.NewRow();
                dRow[0] = txtLoja.Text;
                dRow[1] = txtLink.Text;

                dt.Rows.Add(dRow);
                _dtLinks = dt;

                txtLink.Text = string.Empty;
                txtLoja.Text = string.Empty;

                gvLinks.DataSource = dt;
                gvLinks.DataBind();
            }
        }

        protected void gvLinks_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            System.Data.DataTable dt = (System.Data.DataTable)_dtLinks;
            dt.Rows.RemoveAt(e.RowIndex);
            _dtLinks = dt;
            gvLinks.DataSource = dt;
            gvLinks.DataBind();
        }

        private void CarregarPresentes()
        {
            gvPresentes.DataSource = Presente.List();
            gvPresentes.DataBind();
        }

        protected void gvPresentes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int idPresente = int.Parse(gvPresentes.DataKeys[e.RowIndex].Value.ToString());
            SugestoesPresente.DeleteAll(idPresente);
            Presente.Delete(idPresente);
            Response.Redirect("Presentes.aspx");
        }

        protected void gvPresentes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int idPresente = int.Parse(gvPresentes.DataKeys[e.NewEditIndex].Value.ToString());
            Presente.RetirarReserva(idPresente);
            Response.Redirect("Presentes.aspx");
        }
    }
}